﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Annotations;
using DriveBeamEditor.Enums;
using DriveBeamEditor.Interfaces;
using JBeamLibrary.Tools;

namespace DriveBeamEditor.Controls.General
{
    public partial class NumEdit : UserControl, IEditor, INotifyPropertyChanged
    {
        #region Fields

        private bool _showButtons = true;

        public event EventHandler EditValueChanged;

        #endregion

        #region Properties

        public NumericMaskTypes MaskType
        {
            get { return numTextBox.MaskType; }
            set { numTextBox.MaskType = value; }
        }

        public object EditValue
        {
            get { return numTextBox.EditValue; }
            set
            {
                if (EditValue.SafeEquals(value)) return;
                numTextBox.EditValue = value;
                OnPropertyChanged();
            }
        }

        public bool ShowButtons
        {
            get { return _showButtons; }
            set
            {
                if (_showButtons == value) return;
                _showButtons = value;
                btClear.Visible = value;
                btMax.Visible = value;
            }
        }

        #endregion

        #region CTORs

        public NumEdit()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void btClear_CheckedChanged(object sender, EventArgs e)
        {
            if (btClear.Checked)
            {
                btMax.Checked = false;
                numTextBox.SetToNull();
            }
            else if (!btMax.Checked)
            {
                numTextBox.SetDefault();
            }
        }

        private void btMax_CheckedChanged(object sender, EventArgs e)
        {
            if (btMax.Checked)
            {
                btClear.Checked = false;
                numTextBox.SetToMax();
            }
            else if (!btClear.Checked)
            {
                numTextBox.SetDefault();
            }
        }

        private void numTextBox_EditValueChanged(object sender, EventArgs e)
        {
            //btMax.Checked = numTextBox.IsMax;
            //btClear.Checked = numTextBox.IsNull;

            if (EditValueChanged != null)
                EditValueChanged(this, e);
        }

        #endregion

        #region Methods

        public void SetEditValueSilent(object value)
        {
            numTextBox.SetEditValueSilent(value);
        }

        #endregion

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
